package com.bajiao.admin.controller.industry;

import java.util.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.util.StrUtil;
import com.bajiao.common.api.vo.Result;
import com.bajiao.common.constant._Whole;
import com.bajiao.entity.industry.Industry;
import com.bajiao.entity.store.Store;
import com.bajiao.service.industry.IndustryService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import com.bajiao.admin.base.AdminBaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * @Description: 行业
 * @Author: FanJq
 * @Date: 2024-12-12
 * @Version: V1.0
 */
@Slf4j
@Api(tags = "行业")
@RestController
@RequestMapping("/industry")
public class IndustryController extends AdminBaseController<Industry, IndustryService> {
    @Autowired
    private IndustryService industryService;

    /**
     * @return
     * @description: 分页列表查询
     * @author: FanJq
     * @date: 2024-12-12
     */
    @ApiOperation(value = "行业-分页列表查询", notes = "行业-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(String title) {
        startPage();
        List<Industry> industryList = industryService.lambdaQuery()
                .like(StrUtil.isNotBlank(title), Industry::getTitle, title)
                .eq(Industry::getEnable, _Whole.ZERO)
                .orderByAsc(Industry::getSort)
                .list();
        return Result.ok(getDataTable(industryList));
    }

    @ApiOperation(value = "行业-分页列表查询", notes = "行业-分页列表查询")
    @GetMapping(value = "/select")
    public Result<?> getSelect() {
        List<Industry> industryList = industryService.lambdaQuery()
                .eq(Industry::getEnable, _Whole.ZERO)
                .orderByAsc(Industry::getSort)
                .list();
        return Result.ok(industryList);
    }

    /**
     * @param industry
     * @return
     * @description: 添加
     * @author: FanJq
     * @date: 2024-12-12
     */
    @ApiOperation(value = "行业-添加", notes = "行业-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody Industry industry) {
        industryService.save(industry);
        return Result.ok("添加成功！");
    }

    /**
     * @param industry
     * @return
     * @description: 编辑
     * @author: FanJq
     * @date: 2024-12-12
     */
    @ApiOperation(value = "行业-编辑", notes = "行业-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody Industry industry) {
        industryService.updateById(industry);
        return Result.ok("编辑成功!");
    }

    /**
     * @param id
     * @return
     * @description: 通过id删除
     * @author: FanJq
     * @date: 2024-12-12
     */
    @ApiOperation(value = "行业-通过id删除", notes = "行业-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        industryService.lambdaUpdate()
                .set(Industry::getEnable, 1)
                .eq(Industry::getEnable, 0)
                .eq(Industry::getId, id)
                .update();
        return Result.ok("删除成功!");
    }

    /**
     * @param ids
     * @return
     * @description: 批量删除
     * @author: FanJq
     * @date: 2024-12-12
     */
    @ApiOperation(value = "行业-批量删除", notes = "行业-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.industryService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.ok("批量删除成功！");
    }

    /**
     * @param id
     * @return
     * @description: 通过id查询
     * @author: FanJq
     * @date: 2024-12-12
     */
    @ApiOperation(value = "行业-通过id查询", notes = "行业-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        Industry industry = industryService.getById(id);
        return Result.ok(industry);
    }

    /**
     * @param request
     * @param industry
     * @description: 导出excel
     * @author: FanJq
     * @date: 2024-12-12
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, Industry industry) {
        return super.exportXls(request, industry, Industry.class, "行业");
    }

    /**
     * @param request
     * @param response
     * @return
     * @description: 通过excel导入数据
     * @author: FanJq
     * @date: 2024-12-12
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, Industry.class);
    }
}
